﻿using System.Collections;
using System.ComponentModel;
using System.Windows.Input;

// Author: Miroslav Matula m.matula@alutam.cz 
// Date : 31/8/2010

namespace System.Windows.Controls
{
	public class maDataGrid : DataGrid
	{
		protected override void OnKeyDown(KeyEventArgs e)
		{
			switch (e.Key) {
				//-- PageDown ------------
				case Key.Down:
				case Key.PageDown: {
						bool ok = (this.ItemsSource is IPagedCollectionView) && (this.ItemsSource is ICollectionView);
						if (ok) {
							var icv = (ICollectionView)this.ItemsSource;
							var ipcv = (IPagedCollectionView)this.ItemsSource;
							if (icv.CurrentPosition == (((IList)icv.SourceCollection).Count - 1)) {
								if(ipcv.CanChangePage) {
									(ipcv).MoveToNextPage();
									e.Handled = true;
								}
							}
						}
					}
					break;
				//-- PageUp ------------
				case Key.Up:
				case Key.PageUp: {
						bool ok = (this.ItemsSource is IPagedCollectionView) && (this.ItemsSource is ICollectionView);
						if (ok) {
							var icv = (ICollectionView)this.ItemsSource;
							var ipcv = (IPagedCollectionView)this.ItemsSource;
							if(icv.CurrentPosition == 0) {
								if(ipcv.CanChangePage) {
									ipcv.MoveToPreviousPage();
									e.Handled = true;
								}
							}
						}
					}
					break;
			}
			//-----------
			if (!e.Handled) base.OnKeyDown(e);
		}
	}
}
